/*
xp_socket.h
offer tcp sokect use api and data type define
designed by lunnating

----------------------------
2021-10-21 09:35:59
create file and code first version
*/
#ifndef __XP_SOCKET_H
#define __XP_SOCKET_H



/******************************consig********************************/
#define MAXZ_link       5       //最大链接数
#define BUFF_size       100     //收发缓存大小

/****************************data type define*************************/


//item msg
typedef struct{
    int port;
    char ip[16];
}SOCKET_msg;


//client
typedef struct{
    int sock;
    SOCKET_msg self;
    SOCKET_msg server;
    void (*rec_deal)(SOCKET_msg *,char *,int);
}SOCKET_client;

//accept client
typedef struct{
    int sock;
    char *rec_data;
    int rec_len;
    SOCKET_msg client;
    void *server;
    void (*rec_deal)(void/*ACCEPT_client*/ *,char *,int);
}ACCEPT_client;

//server
typedef struct{
    int sock;
    int close;
    SOCKET_msg self;
    void (*rec_deal)(void/*ACCEPT_client*/ *,char *,int);
    ACCEPT_client *client[MAXZ_link];
}SOCKET_server;



//server create
typedef struct{
    int port;
    SOCKET_server *server;
}SERVER_create;

//client create
typedef struct{
    char ip[16];
    int port;
    SOCKET_client *client;
}CLIENT_create;


/********************************extern api****************************/
extern int xp_socket_client(const char *ip,int port,SOCKET_client *client);    //create a client connect to the server
extern int xp_socket_server(int port,SOCKET_server *server);   //create a server
extern ACCEPT_client *xp_socket_accept(SOCKET_server *server);    //accept client
extern int xp_socket_free(void *obj);   //free socket obj
extern SOCKET_server *xp_server_create(int port,void (*deal_fun)(void *,char *,int));    //create a server
extern SOCKET_client *xp_client_create(char *ip,int port,void (*deal_fun)(SOCKET_msg *,char *,int));    //create client
//read write data
extern int xp_socket_writeString(int sock,const char *str); //write string
extern int xp_socket_writeData(int sock,const void *data,int len);  //write data
extern int xp_socket_readData(int sock, void *data,int len);    //read data
extern int xp_socket_recv(int sock,void *data,int len); //recive data
extern int xp_socket_close(int sock);   //close socket


// log fun api
extern void xp_socket_log_print(void *server,char *str);    //socket log print api
extern void xp_socket_cli_cmd(void *msg,char *str,int len);   //socket cli cmd api


//tools
extern int xp_socket_addrTOint(const char *addr);   //addres string to int
extern char *xp_socket_intTOstring(int addr);   //int address string

#endif
//end of the file
